package com.applozic.mobicomkit.uiwidgets.conversation.messageUtils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.lottery.ruok.lottrey.eventbusObject.SearchRecodeEventMessage;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomUsersResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import property.SharePreferenceProperties;
import utils.Utils;

public class SearchRecordUtils {
    public static String SEARCH_RECORD = "search_recode";
    public static int DATA_TYPE = 2;
    /**
     * 聊天记录
     */
    public static List<GetRoomUsersResponse.ListBean> list = new ArrayList<>();

    public static void add(GetRoomUsersResponse.ListBean temp) {
        try {
            GetRoomUsersResponse.ListBean data = temp.clone();

            if (data == null) {
                return;
            }
            if (!containsKey(data.getUsername())) {
                data.setDataType(DATA_TYPE);
                list.add(0, data);
                SharePreferenceProperties.setObj(SEARCH_RECORD, list);
                EventBus.getDefault().post(new SearchRecodeEventMessage(SearchRecodeEventMessage.MESSAGE_TYPE_ADD, data));
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    public static boolean containsKey(String key) {
        Iterator<GetRoomUsersResponse.ListBean> iterator = list.iterator();
        while (iterator.hasNext()) {
            GetRoomUsersResponse.ListBean next = iterator.next();
            if (next.getUsername().equals(key)) {
                return true;
            }
        }
        return false;
    }

    public static void delete(String key) {
        if (Utils.isEmpty(key)) {
            return;
        }
        if (containsKey(key)) {
            GetRoomUsersResponse.ListBean data = getValue(key);
            list.remove(data);
            SharePreferenceProperties.setObj(SEARCH_RECORD, list);
            EventBus.getDefault().post(new SearchRecodeEventMessage(SearchRecodeEventMessage.MESSAGE_TYPE_DELETE, data));
        }

    }

    private static GetRoomUsersResponse.ListBean getValue(String key) {
        Iterator<GetRoomUsersResponse.ListBean> iterator = list.iterator();
        while (iterator.hasNext()) {
            GetRoomUsersResponse.ListBean next = iterator.next();
            if (next.getUsername() == key) {
                return next;
            }
        }
        return null;
    }

    private static void delete(GetRoomUsersResponse.ListBean listBean) {
        if (listBean != null) {
            delete(listBean.getUsername());
        }
    }

    public static GetRoomUsersResponse.ListBean get(String key) {
        if (Utils.isEmpty(key)) {
            return null;
        }
        if (!containsKey(key)) {
            return null;
        }
        return getValue(key);
    }

    public static void init() {
        if (list != null) {
            list.clear();
        }
        if (!Utils.isEmpty(UserInfo.getUsername())) {
            SEARCH_RECORD = UserInfo.getUsername() + "search_recode";
        }
        String entityStr = SharePreferenceProperties.get(SEARCH_RECORD, "").toString();
        if (!TextUtils.isEmpty(entityStr)) {
            list = JSON.parseArray(entityStr, GetRoomUsersResponse.ListBean.class);
        }
        if (list == null) {
            list = new ArrayList<>();
        }

    }

    public static List<GetRoomUsersResponse.ListBean> getList() {
        List<GetRoomUsersResponse.ListBean> newList = new ArrayList<>();
        newList.addAll(list);
        return newList;
    }

    public static List<GetRoomUsersResponse.ListBean> getSearchHistoryList() {
        List<GetRoomUsersResponse.ListBean> list = getList();
        if (list != null) {
            if (list.size() > 3) {
                return list.subList(0, 3);
            } else {
                return list;
            }
        }
        return null;
    }


    public static List<GetRoomUsersResponse.ListBean> getChatRecodeList() {
        List<GetRoomUsersResponse.ListBean> list = getList();
        if (list != null) {
            if (list.size() > 10) {
                return list.subList(0, 10);
            } else {
                return list;
            }
        }
        return list;
    }

    public static int size() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public static void clearAll() {
        if (list != null) {
            list.clear();
        }
        SharePreferenceProperties.set(SEARCH_RECORD, "");
    }
}
